package bukutelepon.frame;

import bukutelepon.tabel.data;
import bukutelepon.tabel.tabelmodeldata;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.*;

public class Frame extends javax.swing.JFrame
{
    private tabelmodeldata modelData;
    private Connection connection;
    private Statement statement;

    public Frame()
    {
        super("Aplikasi Sederhana Buku Telepon");
        initComponents();
        initForm();
        isi();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tNomer = new javax.swing.JTextField();
        tNomer.setDocument(new onlyDigit().getOnlyDigit());
        tNama = new javax.swing.JTextField();
        tAlamat = new javax.swing.JTextField();
        bInsert = new javax.swing.JButton();
        bUpdate = new javax.swing.JButton();
        bDelete = new javax.swing.JButton();
        bSimpan = new javax.swing.JButton();
        bBatal = new javax.swing.JButton();
        tCari = new javax.swing.JTextField();
        bCari = new javax.swing.JButton();
        bReset = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cbfilter = new javax.swing.JComboBox();
        bReload = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelData = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("NOMER");

        jLabel2.setText("NAMA");

        jLabel3.setText("ALAMAT");

        tNomer.setName("tNomer"); // NOI18N

        bInsert.setText("Insert");
        bInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bInsertActionPerformed(evt);
            }
        });

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        bDelete.setText("Delete");
        bDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDeleteActionPerformed(evt);
            }
        });

        bSimpan.setText("Simpan");
        bSimpan.setEnabled(false);
        bSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSimpanActionPerformed(evt);
            }
        });

        bBatal.setText("Batal");
        bBatal.setEnabled(false);
        bBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBatalActionPerformed(evt);
            }
        });

        tCari.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                tCariInputMethodTextChanged(evt);
            }
        });

        bCari.setText("Cari");
        bCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCariActionPerformed(evt);
            }
        });

        bReset.setText("Reset");
        bReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bResetActionPerformed(evt);
            }
        });

        jLabel4.setText("Pencarian Berdasarkan : ");

        bReload.setText("Bersihkan Pencarian");
        bReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bReloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(bInsert, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(bDelete, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tNama, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                    .addComponent(tAlamat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                    .addComponent(tNomer, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bSimpan)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(bReset, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bBatal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)))
                        .addGap(47, 47, 47)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cbfilter, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(tCari))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(bCari)
                                .addGap(18, 18, 18)
                                .addComponent(bReload)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tNomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bInsert)
                        .addComponent(bReset))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbfilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bUpdate)
                    .addComponent(bBatal)
                    .addComponent(tCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bDelete)
                    .addComponent(bSimpan)
                    .addComponent(bReload)
                    .addComponent(bCari))
                .addGap(29, 29, 29))
        );

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.BorderLayout());

        tabelData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelData.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelDataMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelData);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(447, 447, 447))
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void tabelDataMouseClicked(java.awt.event.MouseEvent evt) {
        aksiTabel();
    }

    private void bInsertActionPerformed(java.awt.event.ActionEvent evt) {
        insert();
    }

    private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        update();
    }

    private void bDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        delete();
    }

    private void bSimpanActionPerformed(java.awt.event.ActionEvent evt) {
        simpan();
    }

    private void bBatalActionPerformed(java.awt.event.ActionEvent evt) {
        batal();
    }

    private void bResetActionPerformed(java.awt.event.ActionEvent evt) {
       if (bInsert.isEnabled()==false)
       {
           clearForm();
       }

       if (bUpdate.isEnabled()==false)
       {
           tNama.setText("");
           tAlamat.setText("");
       }
    }

    private void tCariInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {

    }

    private void bCariActionPerformed(java.awt.event.ActionEvent evt) {
      try
        {
            int row = tabelData.getRowCount();
            for (int i = 0; i < row; i++)
            {
                modelData.delete(0, row);
            }
            if (cbfilter.getSelectedItem()=="Nama")
            {
                String sql = "select * from data where nama like '%"+tCari.getText().trim()+"%' ";
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next())
                {
                    data data = new data();
                    data.setnomer(rs.getString(2));
                    data.setNama(rs.getString(3));
                    data.setAlamat(rs.getString(4));
                    modelData.add(data);
                }
            }
           if (cbfilter.getSelectedItem()=="Nomer")
            {
                String sql = "select * from data where nomer like '%"+tCari.getText().trim()+"%' ";
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next())
                {
                    data data = new data();
                    data.setnomer(rs.getString(2));
                    data.setNama(rs.getString(3));
                    data.setAlamat(rs.getString(4));
                    modelData.add(data);
                }
            }
           if (cbfilter.getSelectedItem()=="Alamat")
            {
                String sql = "select * from data where alamat like '%"+tCari.getText().trim()+"%' ";
                ResultSet rs = statement.executeQuery(sql);
                while (rs.next())
                {
                    data data = new data();
                    data.setnomer(rs.getString(2));
                    data.setNama(rs.getString(3));
                    data.setAlamat(rs.getString(4));
                    modelData.add(data);
                }
            }
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex,"Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void bReloadActionPerformed(java.awt.event.ActionEvent evt) {
        refresh();
        tCari.setText("");
    }


    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Frame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton bBatal;
    private javax.swing.JButton bCari;
    private javax.swing.JButton bDelete;
    private javax.swing.JButton bInsert;
    private javax.swing.JButton bReload;
    private javax.swing.JButton bReset;
    private javax.swing.JButton bSimpan;
    private javax.swing.JButton bUpdate;
    private javax.swing.JComboBox cbfilter;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tAlamat;
    private javax.swing.JTextField tCari;
    private javax.swing.JTextField tNama;
    private javax.swing.JTextField tNomer;
    private javax.swing.JTable tabelData;
    String id,cek,nama, ID,awal;
    // End of variables declaration

    private void initForm()
    {
        modelData = new tabelmodeldata();
        tabelData.setModel(modelData);

        setLocationRelativeTo(this);
        awal();
        openDB();
        refresh();
    }

    private void openDB()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost/buku_telepon", "root", "admin");
            statement = connection.createStatement();
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex, "Error",JOptionPane.ERROR_MESSAGE);
        }
        catch (ClassNotFoundException ex)
        {
            JOptionPane.showMessageDialog(null, ex, "Error",JOptionPane.ERROR_MESSAGE);
        }
    }

    private void insert()
    {
        if (bUpdate.isEnabled()==false)
        {
           JOptionPane.showMessageDialog(null, "Untuk Insert, Klik Batal Terlebih Dahulu");
        }

        if (bUpdate.isEnabled()==true)
        {
            tabelData.setEnabled(false);
            clearForm();
            input();
            bInsert.setEnabled(false);
            bDelete.setEnabled(false);
            bBatal.setEnabled(true);
            bSimpan.setEnabled(true);
        }
    }

    private void update()
    {
        String notnull = tNomer.getText();
        if (bInsert.isEnabled()==false)
        {
            JOptionPane.showMessageDialog(null, "untuk mengupdate data, klik Batal Terlebih Dahulu. Kemudian Pilih data Yang akan DiUpdate");
        }
        if (bInsert.isEnabled()==true)
        {
            if (tNomer.getText().isEmpty()==true)
            {
                JOptionPane.showMessageDialog(null, "untuk mengupdate data, Pilih dulu data Yang akan DiUpdate");
            }

            if (tNomer.getText().isEmpty()==false)
            {
                tabelData.setEnabled(false);
                tNomer.setEditable(true);
                tNama.setEditable(true);
                tAlamat.setEditable(true);
                bUpdate.setEnabled(false);
                bDelete.setEnabled(false);
                bBatal.setEnabled(true);
                bSimpan.setEnabled(true);
            }
        }
    }
class onlyDigit
{
    public onlyDigit()
    {
    }
    /** methode ini dipakai untuk mengeset dan mendapatkan filter
     *  @return mengembalikan filter bertype PlainDocument
     *  Menyaring Hanya karakter angka (Digit) yang bisa dimasukkan
     */
    public PlainDocument getOnlyDigit()
    {
      PlainDocument filterDigit = new PlainDocument()
      {
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
        {
          StringBuffer buffer = new StringBuffer();
          int s = 0;
          char[] dataInput = str.toCharArray();
          // Memeriksa semua data yang dimasukkan
          for (int i = 0; i < dataInput.length; i++)
          {
            // Menyaring Apakah data masukkan berupa DIGIT ??
            boolean isOnlyDigit = Character.isDigit(dataInput[i]);
            if (isOnlyDigit == true)
            {
              dataInput[s] = dataInput[i];
              s++;
            }
          }
          buffer.append(dataInput, 0, s);
          super.insertString(offs, new String(buffer), a);
        }
      };
      return filterDigit;
    }
  }

    private void simpan()
    {
        if(tNomer.getText().trim().isEmpty())
        {
          JOptionPane.showMessageDialog(null, "Nomer Telepon Masih Kosong");
        }
        else if (tNama.getText().trim().isEmpty())
        {
          JOptionPane.showMessageDialog(null, "Nama Masih Kosong");
        }
        else
        {
            cek();
            if (bInsert.isEnabled()==false)
            {
                try
                {
                    if (tNomer.getText().equals(cek))
                    {
                        JOptionPane.showMessageDialog(null, "Nomer "+tNomer.getText()+" sudah dipakai oleh "+nama+". Cek kembali data anda");
                    }
                    if (!tNomer.getText().equals(cek))
                    {
                        String sql = "insert into data (nomer,nama,alamat) values ('" + tNomer.getText() + "','" + tNama.getText() + "','" + tAlamat.getText() + "')";
                        statement.executeUpdate(sql);
                        JOptionPane.showMessageDialog(null, "Data berhasil disimpan");
                        bInsert.setEnabled(true);
                        tabelData.setEnabled(true);
                        refresh();
                        clearForm();
                        awal();
                        bDelete.setEnabled(true);
                        bSimpan.setEnabled(false);
                        bBatal.setEnabled(false);
                    }
                }
                catch (SQLException ex)
                {
                        JOptionPane.showMessageDialog(null, ex, "Error",JOptionPane.ERROR_MESSAGE);
                }
            }


            if (bUpdate.isEnabled()==false)
            {
                if (!tNomer.getText().equals(awal))
                {
                    if (tNomer.getText().equals(cek))
                    {
                        JOptionPane.showMessageDialog(null, "Nomer "+tNomer.getText()+" sudah dipakai oleh "+nama+". Cek kembali data anda");
                    }
                    if (!tNomer.getText().equals(cek))
                    {
                    doUpdate();
                    }
                }
                if (tNomer.getText().equals(awal))
                {
                    doUpdate();
                }
            }
        }
    }
    


    private void delete()
    {
        try
        {
            int app, result;
            if ((app = JOptionPane.showConfirmDialog(null, "Hapus data " + tNama.getText() + " ?", "Perhatian",
            JOptionPane.YES_NO_OPTION)) == 0)
            {
                String sql = "delete from data where nomer = '" + tNomer.getText() + "'";
                result = statement.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "Data berhasil dihapus");
                refresh();
                clearForm();
                awal();
            }
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex, "Error",JOptionPane.ERROR_MESSAGE);
        }
    }


    private void aksiTabel()
    {
        if(tabelData.isEnabled()==false)
        {
           JOptionPane.showMessageDialog(null, "Anda Tidak Bisa Memilih data saat proses Update atau Insert.\n" +
                   "Untuk Memilih data pada tabel, klik batal atau simpan terlebih dahulu.");
        }
        if (tabelData.isEnabled()==true)
        {
            awal();
            tabelData.getSelectionModel().addListSelectionListener(new ListSelectionListener()
                {
                    public void valueChanged(ListSelectionEvent e)
                    {
                        int baris = tabelData.getSelectedRow();
                        if (baris != -1)
                        {
                            data anggota = modelData.get(baris);
                            id=anggota.getid();
                            tNomer.setText(anggota.getnomer());
                            tNama.setText(anggota.getNama());
                            tAlamat.setText(anggota.getAlamat());
                            awal=tNomer.getText();
                        }
                    }
                });
        }
    }

    private void batal()
    {
       int app, result;
       if ((app = JOptionPane.showConfirmDialog(null, "Anda ingin membatalkan?", "Perhatian", JOptionPane.YES_NO_OPTION)) == 0)
                    {
                        tabelData.setEnabled(true);
                        bInsert.setEnabled(true);
                        bUpdate.setEnabled(true);
                        bDelete.setEnabled(true);
                        bSimpan.setEnabled(false);
                        awal();
                        refresh();
                        clearForm();
                    }
    }


    private void clearForm()
    {
        tNomer.setText("");
        tNama.setText("");
        tAlamat.setText("");
    }

    private void refresh()
    {
        try
        {
            int row = tabelData.getRowCount();
            for (int i = 0; i < row; i++)
            {
                modelData.delete(0, row);
            }
            String sql = "select * from data";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next())
            {
                data data = new data();
                data.setid(rs.getString(1));
                data.setnomer(rs.getString(2));
                data.setNama(rs.getString(3));
                data.setAlamat(rs.getString(4));
                modelData.add(data);
            }
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex,"Error", JOptionPane.ERROR_MESSAGE);
        }
    }

private void cek()
    {
        try
        {
            String Sql = "select * from data where nomer = '"+tNomer.getText()+"'";
            ResultSet rs = statement.executeQuery(Sql);
            while (rs.next())
            {
                ID=rs.getString(1);
                nama=rs.getString(3);
                cek=rs.getString(2);
            }
        }
        catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null, ex,"Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void  awal()
    {
       tNomer.setEditable(false);
       tAlamat.setEditable(false);
       tNama.setEditable(false);
    }

    private void input ()
    {
       tNomer.setEditable(true);
       tAlamat.setEditable(true);
       tNama.setEditable(true);
    }

    private void isi()
    {
        cbfilter.addItem("Nama");
        cbfilter.addItem("Nomer");
        cbfilter.addItem("Alamat");
    }
    private void doUpdate()
    {
        try
                {
                    int app, result;
                    if ((app = JOptionPane.showConfirmDialog(null, "Ubah data " + tNama.getText() + " ?", "Perhatian", JOptionPane.YES_NO_OPTION)) == 0)
                    {
                        String sql = "update data set nama = '" + tNama.getText() + "', alamat = '" + tAlamat.getText() + "', nomer = '" + tNomer.getText() + "' where id='"+id+"'";
                        result = statement.executeUpdate(sql);
                        JOptionPane.showMessageDialog(null, "Data berhasil diubah");
                        bUpdate.setEnabled(true);
                        tabelData.setEnabled(true);
                        refresh();
                        clearForm();
                        awal();
                        bDelete.setEnabled(true);
                        bSimpan.setEnabled(false);
                        bBatal.setEnabled(false);
                    }
                }
                catch (SQLException ex)
                {
                    JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
                }
    }
}


